<?php

namespace IDock\Insurance\LIHI\Request;

use IDock\Insurance\LIHI\LIHIEnums;
use IDock\Insurance\LIHI\Request\AllLIHIRequest;
use IDock\Insurance\LIHI\Response\UnderwritingResponse;
use IDock\Visitor\Premium\PremiumInquiryVisitor;
use IDock\Visitor\SpecialClausesVisitor;

class UnderwritingRequest extends AllLIHIRequest
{
    public $route = 'insure/preminumCommit';

    protected $response = UnderwritingResponse::class;

    protected $urlTip = '提交核保';

    /*** @var  PremiumInquiryVisitor $visitor */
    protected $visitor;

    public function generate(): array
    {
        $this->flowId = $this->visitor->getExtInfoVisitor()->argc['flowId'] ?? '';
        $arr = [
            'tprptCarOwnerDto' => $this->getCarOwnerData(),
            'tprptApplicantDto' => $this->getApplicant(),
            'tprptInsuredDto' => $this->getInsured(),
            'remark' => $this->visitor->getExtInfoVisitor()->underwriteMsg,
        ];
        if(!empty($this->visitor->getUnderwritingVisitor()->specialClausesList)){
            $arr['tprptEngageDTOList'] = [];
            $i = 1;
            foreach ($this->visitor->getUnderwritingVisitor()->specialClausesList as $value){
                /**@var SpecialClausesVisitor $value * */
                $arr['tprptEngageDTOList'][] = [
                    'riskCode' => $value->riskType == 'VCI' ? '0502' : '0508',
                    'engageSerialNo' => $i,
                    'clauseCode' => $value->clauseCode,
                    'clausesName' => $value->clauseName,
                    'clausesContext' => $value->clauseContent,
                ];
                $i ++;
            }
        }
        return $arr;
    }

    private function getCarOwnerData()
    {
        $owner = $this->visitor->getOwnerVisitor();
        return [
            'name' => $owner->name,
            'natureOfRole' => $owner->typeCode == 'R1' ? '1' : '2',
            'idType' => LIHIEnums::$certificateTypeCode[$owner->certificateTypeCode] ?? '',
            'idNo' => $owner->certificateNo,
            'cellPhoneNo' => $owner->mobile,
            'address' => $owner->address,
            'email' => $owner->email,
        ];
    }

    private function getApplicant()
    {
        $model = $this->visitor->getApplicantVisitor();
        return [
            'name' => $model->name,
            'natureOfRole' => $model->typeCode == 'R1' ? '1' : '2',
            'idType' => LIHIEnums::$certificateTypeCode[$model->certificateTypeCode] ?? '',
            'idNo' => $model->certificateNo,
            'cellPhoneNo' => $model->mobile,
            'address' => $model->address,
            'email' => $model->email,
        ];
    }

    private function getInsured()
    {
        $model = $this->visitor->getInsuredVisitor();
        return [
            'name' => $model->name,
            'natureOfRole' => $model->typeCode == 'R1' ? '1' : '2',
            'idType' => LIHIEnums::$certificateTypeCode[$model->certificateTypeCode] ?? '',
            'idNo' => $model->certificateNo,
            'cellPhoneNo' => $model->mobile,
            'address' => $model->address,
            'email' => $model->email,
        ];
    }




}
